<?php
/**
 *      [Haidao] (C)2013-2099 Dmibox Science and technology co., LTD.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      http://www.haidao.la
 *      tel:400-600-2042
 */
class member_group_service extends service {
    protected $count;
    protected $pages;

    public function __construct() {
        $this->logic = model('member/member_group', 'logic');
        $this->table = model('member/member_group');
	}

    public function add($params = array()) {
        $result = $this->table->update($params);
        if(!$result) {
            $this->error = $this->table->getError();
            return FALSE;
        }
        return $this->table->getLastInsID();
    }

    public function edit($params = array()) {
        $result = $this->table->update($params);
        if($result === false) {
            $this->error = $this->table->getError();
            return FALSE;
        }
        return TRUE;
    }

    /**
     * 查询单个会员信息
     * @param int $id
     * @return mixed
     */
    public function fetch_by_id($id) {
        $r = $this->table->find($id);
        if(!$r) {
            $this->error = '_SELECT_NOT_EXIST_';
            return FALSE;
        }
        return $r;
    }

    /**
     * 删除指定会员
     * @param type $id
     */
    public function delete_by_id($id) {
        $ids = (array) $id;
        foreach($ids AS $id) {
            if($this->table->delete($id)) {
                /* 删除财务流水 */
                model('member_log')->where(array("mid" => $id))->delete();
                /* 删除收货地址 */
                model('member_address')->where(array("mid" => $id))->delete();
            }
        }
        return TRUE;
    }

    /**
     * 变更用户账户
     * @param int $mid
     * @param string $type
     * @param int $num
     * @param boolean $iswritelog
     * @return boolean 状态
     */
    public function change_account($mid, $field = 'money',$num, $msg = '',$iswritelog = TRUE) {
        $fields = array('money', 'exp', 'integral');
        if(!in_array($field, $fields)) {
            $this->error = '_PARAM_ERROR_';
            return FALSE;
        }
        $result = $this->table->where(array('id' => $mid))->setField($field, array("exp", $field.$num));
        if($result === FALSE) {
            $this->error = '_OPERATION_WRONG_';
            return FALSE;
        }
        if($iswritelog === TRUE) {
            $log_info = array(
                'mid'      => $mid,
                'value'    => $num,
                'type'     => $field,
                'msg'      => $msg,
                'dateline' => TIMESTAMP,
                'admin_id' => (defined('IN_ADMIN')) ? ADMIN_ID : 0
            );
            model('member_log', 'member')->update($log_info);
        }
        return TRUE;
    }
}